package com.htthorizon.model.controller;

import cn.hutool.json.JSONObject;
import com.htthorizon.common.core.constant.SecurityConstants;
import com.htthorizon.common.core.domain.R;
import com.htthorizon.common.core.utils.ip.IpUtils;
import com.htthorizon.common.log.annotation.Log;
import com.htthorizon.common.log.enums.BusinessType;
import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.system.api.RemoteLogService;
import com.htthorizon.system.api.domain.Config;
import com.htthorizon.model.service.IConfigService;
import com.htthorizon.system.api.domain.SysOperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

/**
 * @author Brilliant
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    private IConfigService configService;

    @Autowired
    private RemoteLogService remoteLogService;

    @Autowired
    private RedisService redisService;

    @GetMapping("/get")
    public R get(){
      Config config= configService.getOne();
      return R.ok(config);
    }

    @PostMapping("/edit")
//    @Log(title = "系统管理/系统参数配置", businessType = BusinessType.UPDATE)
    public R edit(@RequestBody Config config) throws Exception {
       boolean result= configService.updateByPrimaryKey(config);
        SysOperLog build = SysOperLog.build("tanggeyan", IpUtils.getIpAddr(), "系统管理/系统参数配置","POST","/config/edit");
        build.setLevel(1);
        build.setStatus(0);
        build.setBusinessType(2);
        build.setOperParam(config.toString());
        build.setOperUrl("/config/edit");
        JSONObject entries = new JSONObject();
        entries.set("msg","操作成功");
        build.setJsonResult(entries.toString());
        redisService.setCacheObject("log:"+build.getCreateTime(),build);
        return R.ok(result);
    }
}
